import { Component, OnInit, ElementRef } from '@angular/core';
import { firstLayerAnimate } from "../../animations/firstLayerAnimate";
import { secondLayerAnimate } from "../../animations/secondLayerAnimate";
import { UserService } from "../../service/userService";
import { MessageService } from 'primeng/api';
import { CookieService } from 'ngx-cookie-service';
import { INT_TYPE } from '@angular/compiler/src/output/output_ast';
import {MyEncrypt} from '../../utils/my-encrypt';

let LOGIN_INTERVAL = 10000;  // ms

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css'],
  animations: [firstLayerAnimate,secondLayerAnimate]
})
export class UserComponent implements OnInit {
  _isTrue2 = true;
  _isTrue3 = true;
  boxState2 = 'left';
  boxState3 = 'left';
  display = false;
  isLogin = false;
  ableToLogin = true;  // 用于防止频繁登录
  loginUserName:string;
  isFeedback:boolean;
  expireTime =  2*60*60*1000;// cookie过期时间两个小时 2*60*60*1000

  username:string = "";
  password:string = "";
  text:string;

  loginbox:any;

  constructor(public elementRef: ElementRef,
              public userService:UserService,
              private msgService:MessageService,
              private cookieService: CookieService) {
   }

  ngOnInit(): void {
    this.checkLogin();
  }

  showoptions(): void {
    if (this._isTrue2) {
      this.boxState2 = 'right';
    } else {
      this.boxState2 = 'left';
    }
    this._isTrue2 = !this._isTrue2;


    if(this.cookieService.get("auth").indexOf("admin")!=-1){
      if (this._isTrue3){
        this.boxState3 = 'right';
      } else {
        this.boxState3 = 'left';
      }
      this._isTrue3 = !this._isTrue3;
    }
  }

  login(){
    if(!this.ableToLogin){
      this.msgService.add({severity:'error', summary:'登录过于频繁', detail:'两次登录间应间隔'+LOGIN_INTERVAL/1000+'s'});
      return;
    }
    if(this.username == ""){
      this.text = "请输入用户名";
      return;
    }
    if(this.password == ""){
      this.text = "请输入密码";
      return;
    }
    this.text = "";
    this.userService.UserLogin(this.username, MyEncrypt(this.password)).subscribe(response=>{
      console.log(response);
      if(response != null){
        this.msgService.add({severity:'success', summary: '登录成功', detail:'登录成功'});
        //set cookie
        this.cookieService.set("userId",response.id,new Date(new Date().getTime() + this.expireTime));
        this.cookieService.set("username",response.username,new Date(new Date().getTime() + this.expireTime));
        this.cookieService.set("nickname",response.nickname,new Date(new Date().getTime() + this.expireTime));
        this.cookieService.set("auth",response.authority,new Date(new Date().getTime() + this.expireTime));
        //update params
        this.isLogin = true;
        this.loginUserName = response.nickname;
        //close the login window
        this.display = false;
        // 防止连续重复操作
        this.ableToLogin = false;
        setTimeout(()=>{
          this.ableToLogin = true;
        }, LOGIN_INTERVAL);
      }
      else{
        this.msgService.add({severity:'error', summary: '登录失败', detail:'登录失败'});
        this.password = "";
      }
    });
  }

  onHide(){
    this.cleanup();
    this.text= "";
  }

  showWin(){
    this.display = true;
  }

  cleanup(){
    this.username = "";
    this.password = "";
  }

  logout(){
    this.showoptions();
    //delete cookie
    this.cookieService.delete("userId");
    this.cookieService.delete("username");
    this.cookieService.delete("nickname");
    this.cookieService.delete("auth");
    //update params
    this.isLogin = false;
    this.loginUserName ="";
  }

  checkLogin(){
    console.log("check login")
    console.log(this.cookieService.get("username"));
    this.loginUserName = this.cookieService.get("nickname");
    if(this.loginUserName != null && this.loginUserName != ""){
      this.isLogin = true;
    }
    else{
      this.isLogin = false;
    }
  }
}
